|
|
|
|
|
|
|
As you can see from this code, the age is being calculated using the built-in function DATEDIFF. Then the age is compared to the AgeLimit property value and if it is less than the age limit, the UnderAge event is triggered; otherwise, the OverAge event is triggered. You must do one last modification to the class code: Remove the Property Let routine for each property that you want to be read-only. |
|
|
|
|
|
|
|
|
Now, to use the class, you will have to add a few lines of code to define the class in the application, then create a new instance of the class, and finally, you must add the code for the event routine of the class. Add a text box, Masked Edit box, two labels, and a command button to the form in the project as shown in Figure 5.11. This will allow you to enter a birth date and then call the GetAge method. |
|
|
|
|
|
|
|
|
Figure 5.11.
Creating a form to accept a birth date to
pass to the class routine. |
|
|
|
|
|
|
|
|
You can see in this figure that I have also added the UpDown control to work with the age limit text box and allow the user to change the age limit value by clicking the up or down arrows. In addition, I also added some standard property settings for the form and the command button. These will center the form on the screen and set the command button to the default so the user can press Enter to calculate the age. |
|
|
|
|
|
|
|
|
Now, add the code in Listing 5.2 to the form's code and run the application. This code will verify that the data entered in the text boxes has the correct data type, and if the information is okay the CalcAge method of GetAge will be invoked. |
|
|
|
|
|
|
|
|
LISTING 5.2. NEWCLASS.TXTTHE APPLICATION CODE FOR THE CLASS.VBP PROJECT SHOWING HOW TO CREATE AND USE A CLASS. |
|
|
|
 |
|
|
|
|
1: Private WithEvents NewAge As GetAge
2:
3: Private Sub cmdCalcAge_Click()
4: Set NewAge = New GetAge
5: If Not IsNumeric(txtAgeLimit.Text) Then
|
|
|
|
|
|